package blazingbubble.legacy.game.graphics;

import java.io.File;
import java.util.HashMap;

import javax.media.opengl.glu.GLUquadric;
import javax.media.opengl.glu.GLU;
import javax.media.opengl.GL;

import blazingbubble.legacy.game.Bubble;
import spark.graphics.Texture;
import spark.graphics.Renderer;
import spark.exception.LoadingException;
import spark.math.Vector3;
import com.sun.opengl.util.texture.TextureCoords;

public class BubbleRenderer
{
	public static final String RESOURCES_PATH = "resources/";
	private GLUquadric sphere;
	private HashMap<Bubble.Type, Texture> bubbleTextures = new HashMap<Bubble.Type, Texture>();

	public BubbleRenderer()
	{
		sphere = Renderer.getInstance().getGlu().gluNewQuadric();
		Renderer.getInstance().getGlu().gluQuadricNormals(sphere, GLU.GLU_SMOOTH);
		Renderer.getInstance().getGlu().gluQuadricTexture(sphere, true);
	}

	public void initializeWithTestLooks() throws LoadingException
	{
		bubbleTextures.put(Bubble.Type.Red, Texture.newTexture(new File(RESOURCES_PATH + "graphics/fb/bubbles/frozenBubble/red.gif"), true));
		bubbleTextures.put(Bubble.Type.Green, Texture.newTexture(new File(RESOURCES_PATH + "graphics/fb/bubbles/frozenBubble/green.gif"), true));
		bubbleTextures.put(Bubble.Type.Blue, Texture.newTexture(new File(RESOURCES_PATH + "graphics/fb/bubbles/frozenBubble/blue.gif"), true));
		bubbleTextures.put(Bubble.Type.Yellow, Texture.newTexture(new File(RESOURCES_PATH + "graphics/fb/bubbles/frozenBubble/yellow.gif"), true));
		bubbleTextures.put(Bubble.Type.Violet, Texture.newTexture(new File(RESOURCES_PATH + "graphics/fb/bubbles/frozenBubble/violet.gif"), true));
		bubbleTextures.put(Bubble.Type.Orange, Texture.newTexture(new File(RESOURCES_PATH + "graphics/fb/bubbles/frozenBubble/orange.gif"), true));
		bubbleTextures.put(Bubble.Type.Black, Texture.newTexture(new File(RESOURCES_PATH + "graphics/fb/bubbles/frozenBubble/black.gif"), true));
		bubbleTextures.put(Bubble.Type.White, Texture.newTexture(new File(RESOURCES_PATH + "graphics/fb/bubbles/frozenBubble/white.gif"), true));
		bubbleTextures.put(Bubble.Type.Rainbow, Texture.newTexture(new File(RESOURCES_PATH + "graphics/fb/bubbles/frozenBubble/rainbow.gif"), true));
		bubbleTextures.put(Bubble.Type.Blazing, Texture.newTexture(new File(RESOURCES_PATH + "graphics/fb/bubbles/frozenBubble/blazing.gif"), true));
		bubbleTextures.put(Bubble.Type.Ghost, Texture.newTexture(new File(RESOURCES_PATH + "graphics/fb/bubbles/frozenBubble/ghost.gif"), true));
	}

	public void render(Bubble bubble, Vector3 offset)
	{
		Renderer.getInstance().getGl().glPushMatrix();
		Renderer.getInstance().getGl().glTranslatef(offset.x + bubble.getPosition().x, offset.y + bubble.getPosition().y, offset.z + bubble.getPosition().z);

		Texture currentTexture = bubbleTextures.get(bubble.getType());

		currentTexture.bind();
		TextureCoords coords = currentTexture.getImageTexCoords();

		Renderer.getInstance().getGl().glBegin(GL.GL_QUADS);

		float r = Bubble.getRadius();
		if(bubble.debug)
			r /= 2;

		Renderer.getInstance().getGl().glTexCoord2f(coords.left(), coords.bottom());
		Renderer.getInstance().getGl().glVertex3f(-r, -r, 0);

		Renderer.getInstance().getGl().glTexCoord2f(coords.right(), coords.bottom());
		Renderer.getInstance().getGl().glVertex3f(r, -r, 0);

		Renderer.getInstance().getGl().glTexCoord2f(coords.right(), coords.top());
		Renderer.getInstance().getGl().glVertex3f(r, r, 0);

		Renderer.getInstance().getGl().glTexCoord2f(coords.left(), coords.top());
		Renderer.getInstance().getGl().glVertex3f(-r, r, 0);

		Renderer.getInstance().getGl().glEnd();

		Renderer.getInstance().getGl().glPopMatrix();
	}
}
